home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8324 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.clark.net!usenet
  2. From: yom@clark.net (yom)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: more problems with qsort
  5. Date: 3 Mar 1996 05:19:51 GMT
  6. Organization: home
  7. Message-ID: <4hba5n$2ga@clarknet.clark.net>
  8. References: <177399702S86.JW1675A@american.edu> <4h0j9e$ng5@clarknet.clark.net> <4h81m0$avr@solutions.solon.com>
  9. NNTP-Posting-Host: yom.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <4h81m0$avr@solutions.solon.com>, seebs@solutions.solon.com 
  15. says...
  16. >
  17. >In article <4h0j9e$ng5@clarknet.clark.net>, yom <yom@clark.net> wrote:
  18. >>Since you're trying to sort a char**, the qsort function call must
  19. >>look like this:
  20. >
  21. >>qsort(array,lines,sizeof(char **),(int (*)(void *,void *)) compare);
  22. >
  23. >This is entirely wrong.  If you're trying to sort an array of pointers
  24. >to char (or a char ** acting like an array of char *'s), you must use
  25. >        qsort(ary, nelem, sizeof(array[0]), compare);
  26. >... and array[0] will be a char *, which may be different from a
  27. >char **.
  28.  
  29. Oops.  You're correct.  Thanks for correcting the mistake.
  30.  
  31. >
  32. >>And your compare function must be defined like this:
  33. >
  34. >>int compare(char **a,char **b) {return strcmp(*a,*b);}
  35. >
  36. >No, it really mustn't.  It must be defined like this:
  37. >int compare(const void *a, const void *b) {
  38. >        return strcmp(*(char **) a, *(char **) b);
  39. >}
  40.  
  41. Well I disagree on this one. Results of 1) defining the compare 
  42. function that takes two char** arguments and casting the function call 
  43. to qsort and 2) defining the compare function that takes two void* 
  44. arguments and casting the arguments to char ** in strcmp call are
  45. identical.  Also, I tend to not use "const" because some compilers do
  46. not support it.
  47.  
  48. Song (yom@clark.net)
  49.  
  50.